package org.opens.lambda.future;

import org.junit.Test;

import java.util.concurrent.*;

public class FutureUserJdk {

    @Test
    public void test1() throws ExecutionException, InterruptedException {
        //ExecutorService的作用之一就是用来创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                //这个线程睡眠用来模拟长时间操作造成的线程阻塞.
                Thread.sleep(1000);
                return "i'm finished";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "error";
            }
        });
        System.out.println(future.get());
        //executorService这个类在执行完指定的任务之后会进入阻塞状态, 需要显式的调用shutdown方法终结这线程池.
        //这个例子中执行完指定的任务之后并没有进入阻塞状态, 自己就停掉了, 可能是因为这个线程池是一个单线程池
        executorService.shutdown();
    }

    /**
     * 功能:
     *      验证test1方法中是否因为其中创建的是单线程导致线程池执行完任务之后就会自动停掉.
     * 结果:
     *      1. 这个执行完任务之后也会自动停掉.
     */
    @Test
    public void test2() throws ExecutionException, InterruptedException {
        //ExecutorService的作用之一就是用来创建线程池
        ExecutorService executorService = Executors.newWorkStealingPool();
        Future<String> future = executorService.submit(() -> {
            try {
                //这个线程睡眠用来模拟长时间操作造成的线程阻塞.
                Thread.sleep(1000);
                return "i'm finished";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "error";
            }
        });
        System.out.println(future.get());
    }

    /**
     * 功能:
     *      给异步任务的获取方法加上超时设定.
     * 输出:
     *      超时了, 当做请求失败了.
     *      java.util.concurrent.TimeoutException
     *      	at java.util.concurrent.FutureTask.get(FutureTask.java:205)
     *
     */
    @Test
    public void test3() throws ExecutionException, InterruptedException {
        //ExecutorService的作用之一就是用来创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                Thread.sleep(10000);
                return "i'm finished";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "error";
            }
        });
        //这种方式可以指定这个请求只能在限定的事件内完成, 否则抛出异常.
        try {
            System.out.println(future.get(1, TimeUnit.SECONDS));
        } catch (TimeoutException e) {
            System.out.println("超时了, 当做请求失败了.");
            e.printStackTrace();
        }
        executorService.shutdown();
    }

    @Test
    public void test4() throws ExecutionException, InterruptedException {
        //ExecutorService的作用之一就是用来创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                Thread.sleep(10000);
                return "i'm finished";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "error";
            }
        });

        //等待异步线程结束
        while(!future.isDone()) {
            Thread.sleep(100);
        }
        System.out.println(future.get());
        executorService.shutdown();
    }

    /**
     * 说明:
     *      1. 用main方法测试会发现, 不调用ExecutorService,shutdown方法, 主线程不会自动结束.
     *
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //ExecutorService的作用之一就是用来创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                //这个线程睡眠用来模拟长时间操作造成的线程阻塞.
                Thread.sleep(1000);
                return "i'm finished";
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "error";
            }
        });
        System.out.println(future.get());
    }

}
